
#include <BLabel>
#include <BGridLayout>
#include <BHBoxLayout>
#include <BVBoxLayout>

#include "LayerPanel.h"

LayerPanel::LayerPanel(BWidget* parent) : BGroupBox(parent)
{
	this->setTitle("ImageLayer");
	_imageLayer = 0;

	BGridLayout* textureLayout = new BGridLayout();
	{
		BLabel* blendLabel = new BLabel("blend");
		_blendComboBox = new BComboBox();
		_blendComboBox->addItem("None", BTexture::Blend_None);
		_blendComboBox->addItem("Modulate", BTexture::Blend_Modulate);
		_blendComboBox->addItem("Replace", BTexture::Blend_Replace);
		_blendComboBox->addItem("Decal", BTexture::Blend_Decal);
		_blendComboBox->addItem("Color", BTexture::Blend_Color);

		_blendColorButton = new BButton(Graph_Square);
		_blendColorButton->setIconSize(-1, -1);
		_blendColorButton->setEnabled(false);

		BLabel* filterLabel = new BLabel("filter");
		_filterComboBox = new BComboBox();
		_filterComboBox->addItem("None", BTexture::Filter_None);
		_filterComboBox->addItem("Nearest", BTexture::Filter_Nearest);
		_filterComboBox->addItem("Linear", BTexture::Filter_Linear);

		BLabel* wrapLabel = new BLabel("wrap");
		_wrapComboBox = new BComboBox();
		_wrapComboBox->addItem("None", BTexture::Wrap_None);
		_wrapComboBox->addItem("Clamp", BTexture::Wrap_Clamp);
		_wrapComboBox->addItem("Repeat", BTexture::Wrap_Repeat);

		BLabel* mipmapLabel = new BLabel("mipmap");
		_mipmapCheckBox = new BCheckBox("mipmap");

		textureLayout->setWidget(0, 0, blendLabel);
		textureLayout->setWidget(0, 1, _blendComboBox);
		textureLayout->setWidget(0, 2, _blendColorButton);
		textureLayout->setWidget(1, 0, filterLabel);
		textureLayout->setWidget(1, 1, _filterComboBox);
		textureLayout->setWidget(2, 0, wrapLabel);
		textureLayout->setWidget(2, 1, _wrapComboBox);
		textureLayout->setWidget(3, 0, mipmapLabel);
		textureLayout->setWidget(3, 1, _mipmapCheckBox);
	}

	BGridLayout* imageLayout = new BGridLayout();
	{
		BLabel* fileNameLabel = new BLabel("fileName");
		_fileNameEdit = new BLineEdit();

		BLabel* policyLabel = new BLabel("policy");
		_policyComboBox = new BComboBox();
		_policyComboBox->addItem("Fixed", Policy_Fixed);
		_policyComboBox->addItem("Dynamic", Policy_Dynamic);

		BLabel* formatLabel = new BLabel("format");
		_formatComboBox = new BComboBox();
		_formatComboBox->addItem("RGB", Format_RGB);
		_formatComboBox->addItem("RGBA", Format_RGBA);
		_formatComboBox->addItem("Gray", Format_Gray);
		_formatComboBox->addItem("Alpha", Format_Alpha);
		_formatComboBox->addItem("Gray-Alpha", Format_Gray_Alpha);

		BLabel* widthLabel = new BLabel("width");
		_widthSpinBox = new BSpinBox(0, 10000);

		BLabel* heightLabel = new BLabel("width");
		_heightSpinBox = new BSpinBox(0, 10000);

		imageLayout->setWidget(0, 0, fileNameLabel);
		imageLayout->setWidget(0, 1, _fileNameEdit);
		imageLayout->setWidget(1, 0, policyLabel);
		imageLayout->setWidget(1, 1, _policyComboBox);
		imageLayout->setWidget(2, 0, formatLabel);
		imageLayout->setWidget(2, 1, _formatComboBox);
		imageLayout->setWidget(3, 0, widthLabel);
		imageLayout->setWidget(3, 1, _widthSpinBox);
		imageLayout->setWidget(4, 0, heightLabel);
		imageLayout->setWidget(4, 1, _heightSpinBox);
	}

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addLayout(textureLayout);
	vlayout->addLayout(imageLayout);

	connect(this, Signal_AssetChanged, &LayerPanel::slotAssetChanged);
	connect(_formatComboBox, Signal_ItemClicked, &LayerPanel::slotFormatItemClicked);
	connect(_filterComboBox, Signal_ItemClicked, &LayerPanel::slotFilterItemClicked);
	connect(_widthSpinBox, Signal_EditFinished, &LayerPanel::slotSizeEditFinished);
	connect(_heightSpinBox, Signal_EditFinished, &LayerPanel::slotSizeEditFinished);

	this->setEnabled(false);
}
LayerPanel::~LayerPanel()
{

}

void LayerPanel::slotAssetChanged()
{
	ImageLayer* imageLayer = this->asset();
	if (_imageLayer != imageLayer)
	{
		_imageLayer = imageLayer;
		this->setEnabled(_imageLayer);
		if (_imageLayer)
		{
			BImage* image = _imageLayer->image();
			_blendComboBox->setValue(image->blend());
			_blendColorButton->setColor(image->blendColor());
			_blendColorButton->setEnabled(image->blend() == BTexture::Blend_Color);
			_filterComboBox->setValue(image->filter());
			_wrapComboBox->setValue(image->wrap());
			_mipmapCheckBox->setChecked(image->mipmap());

			_fileNameEdit->setText(image->fileName());
			_policyComboBox->setValue((PolicyFlag)image->policy());
			_formatComboBox->setValue(image->format());
			_widthSpinBox->setValue(image->width());
			_heightSpinBox->setValue(image->height());
		}
		_backupImage = 0;
	}
}
void LayerPanel::slotFormatItemClicked(BObject* object, const BValue& value)
{
	BComboItem* item = value;
	BImage* oldImage = _imageLayer->image();
	Format format = item->value();
	BImage* image = new BImage(oldImage->size(), format);
	BColor color(255);
	for (int x = 0; x < image->width(); x++)
	{
		for (int y = 0; y < image->height(); y++)
		{
			oldImage->pixelColor(x, y, color);
			image->setPixelColor(x, y, color);
		}
	}
	_imageLayer->setImage(image);
}
void LayerPanel::slotFilterItemClicked(BObject* object, const BValue& value)
{
	BComboItem* item = value;
	BTexture::Filter filter = item->asset();
	_imageLayer->image()->setFilter(filter);
}

void LayerPanel::slotSizeEditFinished(BObject* object, const BValue& value)
{
	BImage* image = _imageLayer->image();
	if (_backupImage.empty())
	{
		_backupImage = new BImage(*image);
	}
	int width = _widthSpinBox->value();
	int height = _heightSpinBox->value();
	image->setSize(width, height);
}

